package com.wyq.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.Audience_Q;
import com.wyq.service.AudienceQService;

/**
 * 问卷调查内容
 * @author Fyj
 */
public class QContentManagerAction extends ActionSupport {
	private AudienceQService audienceQService;
	private String formStr;
	
	//问卷调查标是的ID
	private String titleId;
	private String showInfoId;
	
	private String selIds;//选择删除的IDs

	//获取问卷调查内容
	public String getAllQContent() throws Exception {
		ActionContext context = ActionContext.getContext();   
		HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out=response.getWriter();
		
		System.out.println("qTitle:::::" + titleId);
		
		List list = audienceQService.findAllQContentByQTitleId(titleId);
		String jsonStr = "{qList:" + JSONArray.fromObject(list).toString() + "}"; 
		out.print(jsonStr);
		
		return null;
	}
	
	//保存
	public String saveQContent() throws Exception {
		ActionContext context = ActionContext.getContext();   
		HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
		PrintWriter out=response.getWriter();
		
		formStr = java.net.URLDecoder.decode(formStr,"UTF-8");
		//System.out.println("&&&&&&&&&&&&&&&&" + formStr);
		Map qmap = (Map)JSONUtil.deserialize(formStr);
		int flag = -1;
		int saveFlag = -1;
		if(qmap.get("id").equals("")){//新增
			saveFlag = 0;
		}else if(!qmap.get("id").equals("")){
			saveFlag = 1;
		}
			
		try {
			flag = audienceQService.saveAudienceQContent(qmap, saveFlag);
			out.print("{success:true,data:'" + flag + "'}");

		} catch (Exception e) {
			e.printStackTrace(); 
			out.print("{success:false,reason:'保存失败'}");
		}
		return null;
	}
	
	//删除问卷调查内容
	public String deleteQContent() {
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			
			try{
				String[] idArr = selIds.split(",");
				for(int i=0;i<idArr.length;i++){
					if(null != idArr[i] && !"".equals(idArr[i])){
						Audience_Q audienceQ = new Audience_Q();
						audienceQ.setId(Integer.parseInt(idArr[i]));
						audienceQService.removeAudienceQContent(audienceQ);
					}
				}
				out.print("{success:true,data:'操作成功'}");
				
			}catch(Exception e){
				e.printStackTrace();
				out.print("{success:true,data:'保存失败'}");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public void setAudienceQService(AudienceQService audienceQService) {
		this.audienceQService = audienceQService;
	}

	public void setFormStr(String formStr) {
		this.formStr = formStr;
	}

	public void setSelIds(String selIds) {
		this.selIds = selIds;
	}

	public void setShowInfoId(String showInfoId) {
		this.showInfoId = showInfoId;
	}

	public void setTitleId(String titleId) {
		this.titleId = titleId;
	}

	
	
}
